<template>
    <view class="u-p-t-20">
        <order-collapse-item :list="list" scroll @scroll="scroll">
            <template v-slot:status="{item}">
                <view>
                    <view class="order-status">
                        <text style="color: #2879FE;" v-if="item.status == 0">待审核</text>
                        <text style="color: #FC4F75;" v-if="item.status == 1">待付款</text>
                        <text style="color: #2879FE;" v-if="item.status == 2">待财务审核</text>
                        <text style="color: #FF9900;" v-if="item.status == 3">待发货</text>
                        <text style="color: #FF9900;" v-if="item.status == 4">待收货</text>
                        <text v-if="item.status == 5">已完成</text>
                        <text style="color: #999999;" v-if="item.status == 6">交易关闭</text>
                        <text style="color: #999999;" v-if="item.status == 7">交易取消</text>
                    </view>
                    <view class="refund-status" v-if="[1, 2].indexOf(item.refundStatus) > -1">
                        <text v-if="item.refundStatus === 1">(退款中)</text>
                        <text v-if="item.refundStatus === 2">(退款完成)</text>
                    </view>
                </view>
            </template>
            <template v-slot:function="{item}">
                <view class="func-list">
                    <view class="button-bar">
                        <u-button class="btns" shape="circle" size="mini" v-if="item.status == 4 && item.refundStatus != 2" @click.stop="confirmReceipt(item.id)">
                            确认收货
                        </u-button>
                        <u-button class="btns" shape="circle" size="mini" v-if="item.status == 4 && item.type != 1" @click.stop="tracking(item.id)">查询物流</u-button>
                        <u-button class="btns" shape="circle" size="mini" v-if="item.status == 0 || item.status == 1" @click.stop="cancelTheOrder(item)">取消订单</u-button>
                        <u-button class="btns" shape="circle" size="mini" v-if="item.status == 4 && item.type != 1" @click.stop="receiveList(item.id)">收货清单</u-button>
                        <u-button class="btns" @click.stop="rebuy(item)" shape="circle" size="mini">再次购买</u-button>
                        <u-button class="btns" shape="circle" size="mini" v-if="item.status == 1" @click.stop="pay(item)">
                            <text v-if="item.batchPayCode" class="icon u-m-r-10">&#xe66b;</text>去付款
                        </u-button>
                    </view>
                </view>
            </template>
            <template v-slot:functionFold="{ item }">
                <view class="func-list">
                    <view class="button-bar">
                        <u-button class="btns" shape="circle" size="mini" v-if="item.status == 4 && item.refundStatus != 2" @click.stop="confirmReceipt(item.id)">
                            确认收货
                        </u-button>
                        <u-button class="btns" shape="circle" size="mini" v-if="item.status == 4 && item.type != 1" @click.stop="tracking(item.id)">查询物流</u-button>
                        <u-button class="btns" shape="circle" size="mini" v-if="item.status == 0 || item.status == 1" @click.stop="cancelTheOrder(item)">取消订单</u-button>
                        <u-button class="btns" shape="circle" size="mini" v-if="item.status == 4 && item.type != 1" @click.stop="receiveList(item.id)">收货清单</u-button>
                        <u-button class="btns" @click.stop="rebuy(item)" shape="circle" size="mini">再次购买</u-button>
                        <u-button class="btns" shape="circle" size="mini" v-if="item.status == 1" @click.stop="pay(item)">
                            <text v-if="item.batchPayCode" class="icon u-m-r-10">&#xe66b;</text>去付款
                        </u-button>
                    </view>
                </view>
            </template>
        </order-collapse-item>
    </view>
</template>

<script lang="ts" setup>
import { useGlobalStore } from '@/store'

const globalStore = useGlobalStore()

interface Props {
    res?: any
    list?: Array<any>
}
withDefaults(defineProps<Props>(), {
    res: () => ({}),
    list: () => []
})

const emit = defineEmits(['receive', 'cancel', 'scroll'])

/** 确认收货 */
function confirmReceipt(id: string | number) {
    uni.$u.$showModal({
        content: '在确认前,请确认您已收到货!!!',
        success: (res: any) => {
            if (res.confirm) {
                uni.$u.$api.OrderApi.confirmReceipt({ id }).then(() => {
                    uni.$u.toast('确认收货成功!')
                    setTimeout(() => {
                        emit('receive')
                    }, 1500)
                })
            }
        }
    })
}
/**查询物料轨迹 */
function tracking(id: string | number) {
    uni.$u.$MRouter.expressInfo.to({ id })
}
/** 取消订单 */
function cancelTheOrder(id: string | number) {
    emit('cancel', id)
}

/**收货清单 */
function receiveList(id: string | number) {
    uni.$u.$MRouter.receiveList.to({ id })
}

/**重复购买  再次购买 */
function rebuy(item: any) {
    globalStore.permissionCheck().then((res: any) => {
        if (item.type == 1) {
            item.skus = item.skus[0].list
            item.name = item.skus[0].goodsName
            uni.$u.$MRouter.confirmOrder.to({
                type: 1,
                model: JSON.stringify(item)
            })
        } else {
            globalStore.data = dealData(item)
            uni.$u.$MRouter.confirmOrder.to({
                para: JSON.stringify({
                    orderType: item.orderType
                }),
                from: 'manager'
            })
        }
    })
}
/**
 * 处理数据
 * @param {Array} arr
 * @return {Object} 商品对象,以releaseId 为属性名的对象 包含以skuId 为属性名  num为值的 对象
 * {
 *      releaseId:{
 *          skuId:num
 *      }
 * }
 */
function dealData({ skus }: { skus: Array<any> }) {
    let good: any = {}
    Array.isArray(skus) &&
        skus.forEach(item => {
            item.list.forEach((m: any) => {
                if (!good[m.releaseId]) {
                    good[m.releaseId] = {}
                }
                if (!good[m.releaseId][m.skuId]) good[m.releaseId][m.skuId] = m.num
            })
        })
    return good
}

/**去付款 */
function pay(item: any) {
    let para = JSON.stringify({
        id: item.id,
        totalPrice: item.payPrice,
        addTime: item.addTime,
        overtime: item.overtime,
        type: item.type
    })
    uni.$u.$MRouter.checkstand.to({ para })
}

function scroll(id: string|number){
    emit('scroll', id)
}

</script>

<style scoped lang="scss">
@import './common.scss';
</style>
